/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.help.service.impl;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import java.util.Date;
import java.util.List;
import org.crystalrange.help.dao.HelpItemDao;
import org.crystalrange.help.model.HelpItem;
import org.crystalrange.help.service.HelpItemService;
import org.crystalrange.model.User;
import org.crystalrange.service.UserService;

public class HelpItemServiceImpl implements HelpItemService {

    private HelpItemDao helpItemDao;
    private UserService userService;

    public void setHelpItemDao(HelpItemDao helpItemDao) {
        this.helpItemDao = helpItemDao;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public HelpItem create(String username) {
        
        User currentUser = this.userService.findByUsername(username);  
        
        HelpItem helpItem = new HelpItem();
        helpItem.setCreatedBy(currentUser);
        helpItem.setCreatedDate(new Date());
        
        return helpItem;
    }
    
    public boolean save(HelpItem helpItem, String username) {
        
        User currentUser = this.userService.findByUsername(username);   
        
        helpItem.setUpdatedBy(currentUser);
        helpItem.setUpdatedDate(new Date());
        
        return this.helpItemDao.save(helpItem);
    }

    public boolean remove(HelpItem helpItem) {
        return this.helpItemDao.remove(helpItem);
    }

    public HelpItem find(Long id) {
        return this.helpItemDao.find(id);
    }

    public List<HelpItem> findAll() {
        return this.helpItemDao.findAll();
    }
    
    public SearchResult<HelpItem> searchAndCount(Search search) {
        return this.helpItemDao.searchAndCount(search);
    }

    public List<HelpItem> search(Search search) {
        return this.helpItemDao.search(search);
    }
    
    public HelpItem searchUnique(Search search) {
        return this.helpItemDao.searchUnique(search);
    }
}
